package com.wo.cloud.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wo.cloud.admin.dto.RolePermissionDto;
import com.wo.cloud.admin.entity.SysPermission;
import com.wo.cloud.admin.entity.SysRolePermission;
import com.wo.cloud.admin.service.IRolePermissionService;
import com.wo.cloud.admin.util.SysFeignConstant;
import com.wo.cloud.core.entity.Response;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/5/15 22:43
 * Description: 角色权限管理
 */

@RestController
@RequestMapping(SysFeignConstant.SYS_ROLE_PERMISSION_BASE_URL)
@Api(tags = "角色权限管理模块")
public class SysRolePermissionController {

    @Autowired
    private IRolePermissionService rolePermissionService;

    // 新增角色菜单
    @PostMapping("/save")
    public Response create(@RequestBody RolePermissionDto rolePermissionDto) {
        return rolePermissionService.saveRolePermission(rolePermissionDto);
    }


    // 删除权限
    @DeleteMapping("/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        boolean removeById = rolePermissionService.removeById(id);
        if (removeById) {
            return Response.success();
        }
        return Response.fail();
    }

    // 修改权限
    @GetMapping("/{id}")
    public Response<SysRolePermission> getById(@PathVariable("id") String id) {
        SysRolePermission rolePermission = rolePermissionService.getById(id);
        if (ObjectUtil.isNotNull(rolePermission)) {
            return Response.fail("该数据不存在");
        }
        return Response.data(rolePermission);
    }

    // 分页条件查询
    @GetMapping("/page")
    public Response<IPage<SysPermission>> page(Page page) {
        IPage<SysPermission> sysPermissionIPage = rolePermissionService.page(page);
        return Response.data(sysPermissionIPage);
    }

    // 分页列表
    @GetMapping("/list")
    public Response<List<SysRolePermission>> list() {
        List<SysRolePermission> list = rolePermissionService.list();
        return Response.data(list);
    }
}
